/*
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

package ru.ssau.berlin.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Caesar
 */
@Entity
@NamedQueries({
    @NamedQuery(
       name = "findGroupByName",
       query = "SELECT object(g) FROM PersonGroup as g WHERE g.name LIKE :groupName"
    ),
    @NamedQuery(
        name = "findAllGroups",
        query = "SELECT object(g) FROM PersonGroup as g"
    ),
    @NamedQuery(
        name = "findFaculties",
        query = "SELECT object(g) FROM PersonGroup as g " +
            "WHERE g.type.name IN ('Faculty', 'Факультет')"
    ),
    @NamedQuery(
        name = "findDepartments",
        query = "SELECT object(g) FROM PersonGroup as g " +
            "WHERE g.type.name IN ('Department', 'Кафедра')"
    ),
    @NamedQuery(
        name = "findStudentGroups",
        query = "SELECT object(g) FROM PersonGroup as g " +
            "WHERE g.type.name IN ('StudentsGroup', 'Группа студентов')"
    )
})
public class PersonGroup implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private GroupType type;

    @OneToMany(mappedBy = "group")
    private List<GroupParameter> parameters;

    @ManyToMany(mappedBy = "groups")
    private List<Person> persons;

    @ManyToMany
    private List<Event> events;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public GroupType getType() {
        return type;
    }

    public void setType(GroupType type) {
        this.type = type;
    }

    public List<Event> getEvents() {
        return events;
    }

    public List<GroupParameter> getParameters() {
        return parameters;
    }

    public List<Person> getPersons() {
        return persons;
    }

    @PostLoad
    public void postLoad() {
        parameters.size();
        persons.size();
        events.size();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PersonGroup)) {
            return false;
        }
        PersonGroup other = (PersonGroup) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ru.ssau.berlin.entity.PersonGroup[id=" + id + "]";
    }

}
